package game.tileMap.view
{
	
	
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Back;
	import com.greensock.easing.Quad;
	import com.greensock.easing.RoughEase;
	import com.greensock.events.TweenEvent;
	import com.greensock.plugins.ShortRotationPlugin;
	import com.greensock.plugins.TransformAroundCenterPlugin;
	import com.greensock.plugins.TransformAroundPointPlugin;
	import com.greensock.plugins.TransformMatrixPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Transform;
	
	import game.tileMap.model.vo.Character;
	import game.tileMap.model.vo.MapObj;
	import game.tileMap.model.vo.Unit;
	
	import mk.gamemap.model.TileMapModel0;
	import mk.gamemap.view.anime.AnimeClip;
	import mk.gamemap.view.sprite.CharView;
	import mk.gamemap.view.sprite.SpriteView;
	
	import mx.messaging.channels.AMFChannel;
	import mx.validators.ValidationResult;

	public class UnitView extends CharView
	{
		
		
		public static const MOVE_END:String = "move_end";
		
		
		//上一帧是否在移动，便于触发经过（重合）另一sprite时触发事件
		private var lastMoving:Boolean=false;
		
		public var myVo:Unit;
		
		public function UnitView(model:Character)
		{
			super(model);
			
			myVo=model as Unit;
		}
		
		
		private var  stepCount:int=0
		override public function  update():void{
			if (lastMoving){
				char.update();
			}
			stepCount++
			if (stepCount%3==0){
				animeClip.nextFrame();
			}
			
			updateMove()
			if (lastMoving&&!moving){
				
				endMove();
				
			}
			
		}
		
		public function wait():void{
			endMove();
		}
		
		public function endMove():void{
		//	trace("移动结束：X"+char.x)
			char.endMove();
			dispatchEvent(new Event(MOVE_END));
		}
		
		override public function updateMove():void{
			lastMoving=moving;
			super.updateMove();
			
		}
		
		
		
		
		/**
		 *播放战斗动作 
		 * @param dir 2 4 6 8
		 * 
		 */		
		public function playAttack(dir:int):void{
			var spp:DisplayObject=animeClip.displayObj;
			var toPt:Point=new Point;
			
			
			switch(dir){
				case 2:
					toPt.x=spp.x;
					toPt.y=spp.y+20;
					break;
				case 4:
					toPt.x=spp.x-20;
					toPt.y=spp.y;
					break;
				case 6:
					toPt.x=spp.x+20;
					toPt.y=spp.y;
					break;
				case 8:
					toPt.x=spp.x;
					toPt.y=spp.y-20;
					break;
				case 1:
					toPt.x=spp.x-20;
					toPt.y=spp.y+20;
					break;
				case 3:
					toPt.x=spp.x+20;
					toPt.y=spp.y+20;
					break;
				case 7:
					toPt.x=spp.x-20;
					toPt.y=spp.y-20;
					break;
				case 9:
					toPt.x=spp.x+20;
					toPt.y=spp.y-20;
					break;
					
			}
			
			if (dir!=5){
				TweenMax.to(spp, 0.3, {bezierThrough:[{x:toPt.x,y:toPt.y}, {x:spp.x, y:spp.y}], ease:Quad.easeInOut});
				
				
			}else{
				
			
				TweenLite.from(spp, 0.5, {transformAroundCenter:{scaleX:1.5, scaleY:1.5}});
				//TweenMax.from(spp, 1, {transformMatrix:{scaleX:1.5, scaleY:1.5}});
				//TweenLite.from(spp, 1, {scaleX:1.5,scaleY:1.5});
				
				
						
			}
			
			function endHandler():void{
				myVo.isEnd=false;
			}
		}
		
		
	}
}